package com.powergisol.gis.controller;

import com.powergisol.gis.service.TabCoordinateService;
import com.powergisol.gis.util.Result;
import com.powergisol.gis.vo.TabCoordinateVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * Date: 2019/9/16  11:09
 * Description:坐标系管理
 */
@RestController
@RequestMapping("tabCoordinate")
@Api(description = "坐标系管理" ,value = "坐标系管理")
public class TabCoordinateController {
    private static Logger logger = LoggerFactory.getLogger(TabCoordinateController.class);
    @Autowired
    private TabCoordinateService tabCoordinateService;

    @PostMapping("tabCoordinatePage")
    @ApiOperation(value = "坐标系列表", notes = "坐标系列表")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "name", value = "坐标系名称", defaultValue = "1", required = true, paramType = "query"),
            @ApiImplicitParam(dataType = "Integer", name = "page", value = "页数", defaultValue = "1", required = true, paramType = "query"),
            @ApiImplicitParam(dataType = "Integer", name = "limit", value = "条数", defaultValue = "10", required = true, paramType = "query"),
    })
    public Result tabServicePage(TabCoordinateVo vo){
        PageHelper.startPage(vo.getPage(), vo.getLimit());
        PageInfo<TabCoordinateVo> pageInfo = new PageInfo<TabCoordinateVo>(tabCoordinateService.tabCoordinateList(vo));
        Result result = new Result();
        result.setCount((int) pageInfo.getTotal());
        result.setData(pageInfo.getList());
        return result;
    }

    @PostMapping("tabCoordinateList")
    @ApiOperation(value = "获取坐标系下拉框", notes = "获取坐标系下拉框")
    public Result tabCoordinateList(){
        List<Map<String,Object>> list = tabCoordinateService.selectTabCoordinate();
        Result result = new Result();
        result.setData(list);
        return result;
    }

    @PostMapping("saveCoordinate")
    @ApiOperation(value = "新增坐标系列表", notes = "新增坐标系列表")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "name", value = "坐标系名称", defaultValue = "0", required = true, paramType = "query"),
            @ApiImplicitParam(dataType = "String", name = "epsg", value = "坐标系编码", defaultValue = "0", required = true, paramType = "query"),
            @ApiImplicitParam(dataType = "String", name = "unit", value = "单位", defaultValue = "0", required = true, paramType = "query"),
            @ApiImplicitParam(dataType = "String", name = "projectedBounds", value = "米坐标范围", defaultValue = "0", required = true, paramType = "query"),
            @ApiImplicitParam(dataType = "String", name = "worldExtend", value = "经纬度坐标范围", defaultValue = "0", required = true, paramType = "query"),
            @ApiImplicitParam(dataType = "String", name = "wellKnownText", value = "wkt", defaultValue = "0", required = true, paramType = "query"),
            @ApiImplicitParam(dataType = "String", name = "proj4js", value = "JavaScript (Proj4js)", defaultValue = "0", required = true, paramType = "query")
    })
    public Result saveCoordinate(TabCoordinateVo vo){
        Result result = tabCoordinateService.saveCoordinate(vo);
        return result;
    }

    @PostMapping("upCoordinate")
    @ApiOperation(value = "修改坐标系列表", notes = "修改坐标系列表")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Integer", name = "id", value = "id", defaultValue = "0", required = true, paramType = "query"),
            @ApiImplicitParam(dataType = "String", name = "name", value = "坐标系名称", defaultValue = "0", required = true, paramType = "query"),
            @ApiImplicitParam(dataType = "String", name = "epsg", value = "坐标系编码", defaultValue = "0", required = true, paramType = "query"),
            @ApiImplicitParam(dataType = "String", name = "unit", value = "单位", defaultValue = "0", required = true, paramType = "query"),
            @ApiImplicitParam(dataType = "String", name = "projectedBounds", value = "米坐标范围", defaultValue = "0", required = true, paramType = "query"),
            @ApiImplicitParam(dataType = "String", name = "worldExtend", value = "经纬度坐标范围", defaultValue = "0", required = true, paramType = "query"),
            @ApiImplicitParam(dataType = "String", name = "wellKnownText", value = "wkt", defaultValue = "0", required = true, paramType = "query"),
            @ApiImplicitParam(dataType = "String", name = "proj4js", value = "JavaScript (Proj4js)", defaultValue = "0", required = true, paramType = "query")
    })
    public Result upCoordinate(TabCoordinateVo vo){
        Result result = tabCoordinateService.updateCoordinate(vo);
        return result;
    }

    @PostMapping("delCoordinate")
    @ApiOperation(value = "删除坐标系", notes = "删除坐标系")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Integer", name = "id", value = "id", defaultValue = "0", required = true, paramType = "query")
    })
    public Result delCoordinate(Integer id){
        Result result = new Result();
        tabCoordinateService.delCoordinate(id);
        result.success("删除成功");
        return result;
    }

    @PostMapping("expCoordinate")
    public Result expCoordinate(){
        Result result = new Result();
        File file = new File("E:/crs.json");
        String jsonString = null;
        try {
            jsonString = FileUtils.readFileToString(file,"UTF-8");
        } catch (IOException e) {
            e.printStackTrace();
        }

        String str[] = jsonString.replace("[{","").replace("}]","").replace("}","").split(", \\{");
        for (int i=0;i<str.length; i++){
            TabCoordinateVo tabCoordinate = new TabCoordinateVo();
            String attr = str[i];
            String att[] = attr.split(", \"");
            tabCoordinate.setName(att[1].substring(14,att[1].length()-1));
            tabCoordinate.setEpsg(att[0].substring(8,att[0].length()-1));
            tabCoordinate.setWellKnownText(att[2].substring(6,att[2].length()-1));

            String c = att[3];
            if(c.contains("projected")){
                tabCoordinate.setCrsType("2");
            }else if(c.contains("geographic")){
                tabCoordinate.setCrsType("1");
            }
            String a = att[2].substring(6,att[2].length()-1);
            if(!a.equals("-")){
                String b[] =a.split("UNIT");
                if(b.length==3){
                    String unit = b[2].split("]")[0].split(",")[0].replace("[","").replaceAll("\"","");
                    if(unit.contains("m")){
                        unit = "m";
                    }
                    tabCoordinate.setUnit(unit);
                }else if(b.length==2){
                    String unit = b[1].split("]")[0].split(",")[0].replace("[","").replaceAll("\"","");
                    if(unit.contains("m")){
                        unit = "m";
                    }
                    tabCoordinate.setUnit(unit);
                }

            }
            tabCoordinateService.saveCoordinate(tabCoordinate);
        }
        return result;
    }

}
